#include "AnimationManager.h"
#include "../Utility/Renderer/GeometryLoader.h"
#include "../Renderer/VertexBufferManager.h"
#include "../Renderer/IndexBuffer.h"

Animation				AnimationManager::animation;
AnimatedMeshDataPNT*	AnimationManager::playerMesh;

unsigned int			AnimationManager::startVertex;
unsigned int			AnimationManager::startIndex;
unsigned int			AnimationManager::vertCount;
unsigned int			AnimationManager::primitiveCount;
Timer					AnimationManager::timer;

void AnimationManager::Initialize()
{
	LoadAnimation("Res/Animation/Idle.anim");
	timer.Init();
}

void AnimationManager::LoadAnimatedMesh(string _filePath, Mesh& mesh)
{
	GeometryLoader<AnimatedMeshDataPNT>::LoadAnimatedGeometry(_filePath, playerMesh);

	vertCount		= playerMesh->vertices.size();
	startVertex		= VertexBufferManager::m_PosNormTexBuffer.AddVerts(&playerMesh->vertices[0], vertCount);
	startIndex		= IndexBuffer::AddIndices(&playerMesh->indices[0], playerMesh->indices.size());
	primitiveCount	= playerMesh->indices.size()/3;

	VertexBufferManager::m_PosNormTexBuffer.UpdateVerts(&playerMesh->vertices[0], startVertex, vertCount);

	mesh.vertCount		= vertCount;
	mesh.startVertex	= startVertex;		
	mesh.startIndex		= startIndex;
	mesh.primitiveCount = primitiveCount;

	for(unsigned int i = 0; i<playerMesh->joints.size(); ++i)
		playerMesh->joints[i].transformFrame.Update();
}

void AnimationManager::LoadAnimation(string _filePath)
{
	animation.LoadAnimation(_filePath);
}

void AnimationManager::PlayAnimations()
{
	timer.TimeStep();
	float dt = float(timer.GetDeltaTime()/1000.0);
	animation.PlayAnimation(dt);
	animation.ApplySmoothSkinning(playerMesh);
	VertexBufferManager::m_PosNormTexBuffer.UpdateVerts(&animation.skinVerts.vertices[0],startVertex,vertCount);
}